package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.ShanDongDianXinDes;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

/**
 * @ClassName: ShanDongDianXinFlowTask
 * @Description: 山东电信流量充值查询
 * @author CXJ
 * @date 2017年4月10日 上午10:07:04
 * @version 1.0
 */
@Component
public class ShanDongDianXinFlowTask
{
    /**
     * 日志
     */
    private static final Logger log = Logger.getLogger(YunShangFlowTask.class);
    
    @Autowired
    OrderDao orderDao;
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
//    @Scheduled(fixedDelay = 60000)
    public void queryShanDongDXOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 2;
        try
        {
            String providerId = "0000000000";//
            Map<String, Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);// 获取通道
            String defaultParameter = (String)channel.get("default_parameter");// 参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String queryUrl = paramMap.get("queryUrl");// 查询地址
            // 加密key
            String desKey = paramMap.get("desKey");
            // 认证码加密前
            String myCode = paramMap.get("myCode");
            // 编码
            String charset = paramMap.get("charset");
            // 接口调用认证码
            String authenticationCode = ShanDongDianXinDes.encrypt(myCode, desKey);
            // 查询订单中属于指通的折扣
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);// 查询出所有尚通的充值中的订单
            for (int i = 0, size = orderList.size(); i < size; i++)
            {
                Map<String, Object> order = orderList.get(i);
                // 渠道订单号
                String clientNum = order.get("orderId") + "";
                map.put("orderId", clientNum);
                Map<String, String> dataMap = new HashMap<String, String>();
                String dispacthInfo =
                    "{'authenticationCode': '" + authenticationCode + "', 'clientNum':'" + clientNum + "'}";
                dataMap.put("dispatchInfo", dispacthInfo);
                String result = ToolHttp.post4sddx(queryUrl, dataMap, charset);
                if (result != null)
                {
                    JSONObject resultJson = JSONObject.parseObject(result);// 返回信息
                    // 结果码
                    String resultCode = resultJson.get("resultCode") + "";
                    // 错误码
                    String errorCode = resultJson.get("errorCode") + "";
                    // 失败信息
                    String errorMsg = resultJson.get("errorMsg") + "";
                    map.put("resultCode", errorCode + ":" + errorMsg);
                    if ("0".equals(resultCode))
                    {
                        flag = 1;
                    }
                    else
                    {
                        flag = 0;
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("山东电信流量查询出错" + e);
        }
    }
}
